// pages/myCollect/myCollect.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collectArr: [],
    myCollect: [],
    stick: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      collectArr: wx.getStorageSync('collect') || [],
    })

    if (wx.getStorageSync('my-collect').length != wx.getStorageSync('collect').length) {
      this.onGetCollect()
    } else {
      this.setData({
        myCollect: wx.getStorageSync('my-collect') || []
      })
    }
  },

  // 本地没有数据需要调用接口
  onGetCollect() {
    wx.request({
      url: 'http://localhost:2345/msbd/my-collect',
      data: {
        id: this.data.collectArr.join(",")
      },
      success: res => {
        for (let i = 0; i < res.data.data.data.length; i++) {
          res.data.data.data[i].stick = false
        }
        let myCollect = res.data.data.data,
          collect = wx.getStorageSync('my-collect') || []

        if (!collect.length) {

          this.setData({
            myCollect: [...myCollect, ...collect]
          })
          wx.setStorageSync('my-collect', this.data.myCollect)

        } else {

          let arr = [],
            isTrue = false
          collect.forEach((v, i) => {
            myCollect.forEach((item, index) => {
              if (v.id == item.id) {
                myCollect.splice(index,1)
              }
            })
          })
          wx.setStorageSync('my-collect', [...collect, ...myCollect])
          this.setData({
            myCollect: wx.getStorageSync('my-collect')
          })
        }
      }
    })
  },

  // 置顶
  onStick(e) {
    let arr = wx.getStorageSync('my-collect') || [],
      currentIdx = e.currentTarget.dataset.stick
    arr[currentIdx].stick = !arr[currentIdx].stick
    if (arr[currentIdx].stick) {
      let currentStick = arr[currentIdx]
      arr.splice(currentIdx, 1)
      arr.unshift(currentStick)
    }
    wx.setStorageSync('my-collect', arr)
    this.setData({
      myCollect: arr
    })
  },

  onDeleteCollect(e) {
    let currentIdx = e.currentTarget.dataset.delete
    wx.showModal({
      title: '提示',
      content: '确定要删除该收藏吗？',
      success: (res) => {
        if (res.confirm) {
          let myCollect = this.data.myCollect,
            collectArr = this.data.collectArr
          myCollect.splice(currentIdx, 1)
          collectArr.splice(currentIdx, 1)
          this.setData({
            myCollect,
            collectArr
          })
          wx.setStorageSync('collect', collectArr)
          wx.setStorageSync('my-collect', myCollect)
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})